﻿@{
    ViewBag.Title = "Index";

    Layout = "~/Views/Shared/_Index.cshtml";
}

<style>
    thead tr th {
        background-color: #eee;
        line-height: 30px;
        padding-left: 15px;
    }

    tbody tr td {
        line-height: 30px;
        padding-left: 15px;
    }

    .cellvalue {
        width: 90%;
        line-height: 25px;
    }

    .redBord {
        border-color: red;
    }
</style>
<div class="panel panel-default" style="margin-bottom:0;">
    <div class="panel-heading navb-bg">
        门诊退费查询
    </div>
    <div style="padding: 2px;padding-right:20px;">
        <table class="form" style="width:96%;">
            <tr>
                <td class="formTitle">卡号：</td>
                <td class="formValue">
                    <input id="kh" type="text" class="form-control form-control-focus" placeholder="请输入卡号">

                </td>
                <td class="formTitle">从：</td>
                <td class="formValue">

                    <input class="form-control input-wdatepicker" type="text" onfocus="WdatePicker({
                                onpicked: function () {getAge($(this).val());return true;}})" id="ks" value="@DateTime.Now.AddMonths(-1).ToString("yyy-MM-dd")" />
                </td>
                <td class="formTitle">至：</td>
                <td class="formValue">
                    <input class="form-control input-wdatepicker" type="text" onfocus="WdatePicker({
                                onpicked: function () {getAge($(this).val());return true;}})" id="js" value="@DateTime.Now.AddDays(1).ToString("yyy-MM-dd")" />
                </td>
                <td class="formTitle"></td>
                <td class="formValue">
                    <button id="BtnQuery" class="btn btn btn-default btn-md dropdown-text">查询</button>
                </td>
            </tr>
        </table>
    </div>
</div>
<br />
<form id="tabGrid">
    <table class="form" style="border:solid 1px #ddd;" id="tableStyle">
        <thead>
            <tr>
                <th>病人类型</th>
                <th>挂号科室</th>
                <th>医生</th>
                <th>发票号</th>
                <th>记账</th>
                <th>自费</th>
                <th>分类自负</th>
                <th>自负</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td id="brlx"></td>
                <td id="ghks"></td>
                <td id="ys"></td>
                <td id="fph"></td>
                <td id="jz"></td>
                <td id="zifei"></td>
                <td id="flzf"></td>
                <td id="zf"></td>
            </tr>
        </tbody>
    </table>
    <br />
    <div class="gridPanel">
        <table id="gridListTuifei" rel="tabGrid"></table>
    </div>
</form>

<br />
<div class="panel panel-default" style="margin-bottom:0;">
    <div class="panel-heading navb-bg">
        退费
    </div>
    <div style="padding: 2px;padding-right:20px;">
        <table class="form" style="width:96%;">
            <tr>
                <td class="formTitle">自负：</td>
                <td class="formValue">
                    <input class="form-control newtouch_Readonly" type="text" id="zifu" value="" />
                </td>
                <td class="formTitle">应退：</td>
                <td class="formValue">
                    <input class="form-control newtouch_Readonly" type="text" id="yingtui" value="" />
                </td>
                <td class="formTitle"></td>
                <td class="formValue"></td>
                <td class="formTitle"></td>
                <td class="formValue"></td>
            </tr>
        </table>
    </div>
</div>

@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 4, 6 },
    F6Text = "退费"
})

<script>

    $(function() {
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function() {
            initLayout("tabGrid");
        });

        var txt_keyword = document.getElementById('kh');
        txt_keyword.addEventListener('keydown',
            function(event) {
                event = event || window.event;
                if (event.keyCode === 13) {

                    btnQueryFph();
                }
            });

        //清空
        newtouch_event_f4();

        $("#brlx").html("<font color='red'>暂无信息</font>");
        gridListTuifei([]); //退费列表


        //查询
        $("#BtnQuery").click(function() {
            clearFPYSInfo();
            if ($("#kh").val() === "") {
                $.modalAlert("请输入卡号", "warning");
                return;
            }
            btnQueryFph();

        });
    });

    var jsnm; //获取从弹出窗口中得到的结算内码

    //清空发票医生信息
    function clearFPYSInfo() {
        $("#brlx").html("<font color='red'>暂无信息</font>");
        $("#ghks").html("");
        $("#ys").html("");
        $("#fph").html("");
        $("#flzf").html("");
        $("#zf").html("");
        $("#jz").html("");
        $("#zifei").html("");
    }

    function btnQueryFph() {
        var obj = new Object;
        obj.kh = $.trim($("#kh").val());
        obj.startTime = $.trim($("#ks").val());
        obj.endTime = $.trim($("#js").val());
        GetQueryFphAjax(obj);
    }

    //查询发票
    function GetQueryFphAjax(obj) {
        $.loading(true, "正在请求数据..."); 
        $.najax({
            type: "POST",
            url: "@Url.Action("btnQueryFph")",
            data: obj,
        dataType: "json",
        cache: false,
        success: function (ajaxresp) {
            localStorage.setItem("fapiaodata", JSON.stringify(ajaxresp.data));
            $('#gridListFPH').jqGrid("clearGridData");
            $('#gridListTuifei').jqGrid("clearGridData");
            //弹出发票列表窗口
            $.modalOpen({
                id: "QueFPList",
                title: "门诊退费查询",
                url: "/OutpatientManage/Refund/QueFpList?r=" + Math.random(),
                width: "700px",
                height: "600px",
                callBack: function (iframeId) {
                    top.frames[iframeId].PatSearchConfirm(); //在弹出窗口事件
                    //调用查询发票结算信息

                }//窗口点确定的回调函数
            });
            
        },
        errorCallback: function (err) {
            $.loading(false);
        },
        complete: function () {
            isLoadingIIII = false;
            $.loading(false);
        }
    }); 
    }

    function getDetail(jsnm) {
        $.najax({
            type: "POST",
            url: "@Url.Action("GetGridViewMxByJsnm")",
            data: { jsnm: jsnm },
            cache: false,
            dataType: "json",
            success: function (ajaxresp) {
                $('#gridListTuifei').jqGrid("clearGridData");
                gridListTuifei(ajaxresp.data);
                $(".IS_RETURN").click(function () {
                    var czh = $(this).attr("name");
                    if (czh != "0") {
                        var isTrue = $(this).is(':checked');
                        if (isTrue) {
                            $("[name ='" + czh + "' ]:checkbox").prop("checked", "checked");

                        } else {
                            $("[name ='" + czh + "' ]:checkbox").attr("checked", false);
                        }
                    }
                });
            },
            errorCallback: function (err) {
            },
            complete: function () {
                 isLoadingIIII = false;
                $.loading(false);
            }

        });
    }

    //退费列表
    function gridListTuifei(mydata) {
        var $gridList = $("#gridListTuifei");
        $gridList.dataNewGrid({
            height: 180,
            autowidth: false,
            width: $("#tabGrid").width() - 2,
            caption: "退费列表",
            colModel: [
                    { label: 'id', name: 'id', hidden: true },
                    { label: 'CF_MXNM', name: 'CF_MXNM', hidden: true },
                    { label: 'InfoSet', name: 'InfoSet', hidden: true },
                    { label: 'DL', name: 'DL', hidden: true },
                    { label: 'czh', name: 'czh', hidden: true },
                    { label: '处方号', name: 'CHUFANGHAO', width: 150, align: 'left' },
                    { label: '类型', name: 'DLMC', width: 80, align: 'left' },
                    { label: 'sfxm', name: 'SFXM', hidden: true },
                    { label: 'xmnm', name: 'XMNM', hidden: true },
                    { label: 'zfxz', name: 'ZFXZ', hidden: true },
                    { label: 'nbdl', name: 'NBDL', hidden: true },
                    { label: 'ghnm', name: 'GHNM', hidden: true },
                    { label: '项目名称', name: 'YPMC', width: 150, align: 'left' },
                    { label: '单位', name: 'DANWEI', width: 80, align: 'left' },
                    { label: '单价', name: 'DJ', width: 80, align: 'left' },
                    { label: '自负比例', name: 'ZFBL', width: 80, align: 'left' },
                    { label: '数量', name: 'SL', width: 80, align: 'left' },
                    {
                        label: '退数量', name: 'RETURNS_SL', width: 80, align: 'left',
                        formatter: function (cellvalue) {
                            return "<input name='IS_RETURN' type='text' class='tuishuliang' style='height: 30px; width: 35px;line-height: 30px;' checked value='" +
                                cellvalue +
                                "' />";
                        }
                    },
                    { label: '金额', name: 'JE', width: 80, align: 'left' },
                    {
                        label: '退', name: 'IS_RETURN', width: 80, align: 'left', editable: true, edittype: 'checkbox', formatter: function (cellvalue) {
                            var IS_RETURNAndCzh = cellvalue.split(",");
                            return IS_RETURNAndCzh[0] == '1' ? "<input name='" + IS_RETURNAndCzh[1] + "' class='IS_RETURN' type='checkbox' checked='checked' value='' />" : "<input name='" + IS_RETURNAndCzh[0] + "' type='checkbox'    value='' />"
                        }
                    }
            ]
        }, mydata);
        if (mydata.length > 0) {
            $('.ui-jqgrid-bdiv').find(".unwritten").remove();
        }
    }

    //退费
    function Refund() {
        var flag = true;
        var tuifei = $("#gridListTuifei").jqGrid('getGridParam', 'data');
        var IS_RETURN = $("#gridListTuifei").find(".IS_RETURN");
        var RETURNS_SL = $("#gridListTuifei").find(".tuishuliang");

        var refund = 0;
        var count = 0;
        if (RETURNS_SL.length > 0) {
            RETURNS_SL.each(function () {
                var tsl = $(this).val();
                if (tuifei[count].SL < tsl) {
                    $.modalAlert("退数量不能大于数量！", "error");
                    $(this).addClass("redBord");
                    flag = false;

                } else {
                    tuifei[count].RETURNS_SL = tsl;
                }
                count++;
            });
        }
        count = 0;
        if (IS_RETURN.length > 0) {
            IS_RETURN.each(function () {
                var isTrue = $(this).is(':checked');
                if (isTrue) {
                    tuifei[count].IS_RETURN = true;


                } else {
                    tuifei[count].IS_RETURN = false;
                }
                count++;
            });
        } else {
            $.modalAlert("没有选中的退费项目！", "error");
            flag = false;
        }

        for (var j = 0; j < tuifei.length; j++) {
            if (tuifei[j].IS_RETURN) {

                refund = refund + parseFloat(tuifei[j].DJ) * tuifei[j].RETURNS_SL;
            }
        }

        if (flag) {
            var kh = $("#kh").val();
            var refundData = JSON.stringify(tuifei).toString();
            $.modalConfirm("您确定要退费吗？", function (flag) {
                if (flag) {
                    $.loading(true, "正在请求数据...");

                    if (jsnm && kh) {
                        $.najax({
                            type: "POST",
                            async: false,
                            url: "@Url.Action("btnReturn")",
                            data: { kh: kh, jsnm: jsnm, refundData: refundData },
                            dataType: "json",
                            cache: false,
                            success: function (ajaxresp) {
                                var msg = "退费成功！应退金额为：";
                                if (ajaxresp.data) {
                                    msg = msg + $("#yingtui").val();
                                } else {
                                    msg = msg + refund;
                                }
                                $.modalAlert(msg, ajaxresp.state);
                                $("#zifu").val("");
                                $("#yingtui").val("");
                                $('#gridListTuifei').jqGrid("clearGridData");
                                clearFPYSInfo();//清除数据
                            },
                            errorCallback: function (err) {
                                $.loading(false);
                            },
                            complete: function () {
                                $.loading(false);
                            }
                        });
                    } else {
                        $.modalAlert("数据不全，请检查卡号或联系管理员！", "error");
                    }
                }
            }, false);
        }

    }


    //查询结算记录
    function chajilu(jsnm) {
        $.loading(true, "正在请求数据...");
        $.najax({
            type: "POST",
            url: "@Url.Action("GetMZJSByJsnm")",
            data: { jsnm: jsnm },
            dataType: "json",
            cache: false,
            success: function (ajaxresp) {
                if (ajaxresp.state === 'success' && ajaxresp.data != null && ajaxresp.data.length>0) {
                    var result = ajaxresp.data[0];
                    $("#brlx").html(result.BRXZMC);
                    $("#ghks").html(result.KSMC);
                    $("#ys").html(result.RYMC);
                    getDetail(jsnm);

                }
            },
            errorCallback: function (err) {
            },
            complete: function () {
                isLoadingIIII = false;
                $.loading(false);
            }
        });
    }

    ///加载发票和其他费用信息
    function loadFpData(data) {
        jsnm = data.jsnm;
        $("#fph").html(data.fph);
        $("#flzf").html(data.flzffy);
        $("#zf").html(data.zffy);
        $("#jz").html(data.jzfy);
        $("#zifei").html(data.zlfy);
        $("#zifu").val(data.zffy);
        $("#yingtui").val(data.zje);
        chajilu(data.jsnm);
    }

    //快捷键：清除
    function newtouch_event_f4() {
        clearFPYSInfo();
        $('#gridListTuifei').jqGrid("clearGridData");
    }

    //快捷键：F6：退费
    function newtouch_event_f6() {
        Refund();
    }

</script>
